#!/usr/bin/python3

###################################
# 列表推导式
print('列表推导式')
list = [-1, 2, -3, 4]
result = [[i,2*i,abs(i)] for i in list if i%2==0]
print(result)

print('构造不同类型的结果：')
result = {i:abs(i) for i in list}
print(result)

print('双重遍历：')
list2 = [6,6,6]
result = [x*y for x in list for y in list2]
print(result)
print()

###################################
# 遍历技巧
print('遍历技巧')
print('加index：')
for i, v in enumerate(['tic', 'tac', 'toe']):
    print(i, v)

print('zip()使用：')
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip(questions, answers):
    print('What is your {0}?  It is {1}.'.format(q, a))

print('反向遍历：')
for i in reversed(range(1, 10, 2)):
    print(i, end=', ')
print()

print('去重排序：')
data = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
for d in sorted(set(data)):
    print(d, end=', ')
print()
